//
//  DDCheckUpdata.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/9/26.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDCheckUpdata.h"

@implementation DDCheckUpdata

NSDictionary *checkVersionMessage; //获取APP最新的版本信息
NSString *updataMessage; //获取APP的更新内容
UIView *windowShadowView;

+ (instancetype)shareManager
{
    static DDCheckUpdata *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    
    return manager;
}

- (void)checkUpdata
{

    NSString *URLString = [[DDAPIGenerate sharedInstance] API:@"query_latest_version"];
    
    DDNetWorkManager *managerVersion = [DDNetWorkManager shareManager];
    [managerVersion POST:URLString parameters:nil success:^(id responseObject) {
        
        checkVersionMessage = responseObject;
        [self performSelectorOnMainThread:@selector(backToMainToCheck) withObject:self waitUntilDone:YES];
        
    } failure:nil];
}

- (void)backToMainToCheck
{
    /**
     *  检查版本更新
     *
     */
    NSString *appStoreVersion = checkVersionMessage[@"data"][@"latest_version_ios"];
    updataMessage = checkVersionMessage[@"data"][@"update_content_ios"];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *localVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    NSArray *messageList_appStoreVersion = [appStoreVersion componentsSeparatedByString:@"."];
    NSString *appStoreVersion_New = [messageList_appStoreVersion componentsJoinedByString:@""];
    
    NSArray *messageList_localVersion = [localVersion componentsSeparatedByString:@"."];
    NSString *localVersion_New = [messageList_localVersion componentsJoinedByString:@""];
    
    /**
     *  本地的版本号
     */
    float localVersion_float = [localVersion_New floatValue];
    /**
     *  appStore上的版本号
     */
    float appStoreVersion_float = [appStoreVersion_New floatValue];
    
    
    if (appStoreVersion_float <= localVersion_float) {
        /**
         *  没有新版本
         */
        return;
    }else{
        /**
         *  发现新版本
         */
//        [self creatupdataWindow];
        
        WEAKSELF
        [weakSelf ShowZTTiteAlertView:@"应用更新" info:updataMessage sure:^{
            
            NSURL *urlUpdate  = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1119078683"];
            [[UIApplication sharedApplication] openURL:urlUpdate];
            
        } cancel:nil];
    }
}

/**
 *  弹出更新窗口
 */
- (void)creatupdataWindow
{
    windowShadowView = [[UIView alloc] init];
    windowShadowView.userInteractionEnabled = YES;
    windowShadowView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    windowShadowView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.3500];
    AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [app.window.rootViewController.view addSubview:windowShadowView];
    
    
    UIView *inner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 125+40)];
    inner.backgroundColor = [UIColor whiteColor];
    inner.layer.masksToBounds = YES;
    inner.layer.cornerRadius = 10.0;
    inner.center = windowShadowView.center;
    [windowShadowView addSubview:inner];
    
    UILabel *infoLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(inner.bounds), 30)];
    infoLab.textAlignment = NSTextAlignmentCenter;
    infoLab.font = [UIFont systemFontOfSize:17];
    infoLab.textColor = COLOR_FONT_BLACK;
    infoLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    infoLab.text = @"应用更新";
    [inner addSubview:infoLab];
    
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(infoLab.frame), CGRectGetWidth(inner.bounds), 30)];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.textColor = COLOR_FONT_DARK_GRAY;
    messageLabel.text = @"好消息!现在可以免费下载最新版本了";
    messageLabel.font = [UIFont systemFontOfSize:13];
    [inner addSubview:messageLabel];
    
    UILabel *updataContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,CGRectGetMaxY(messageLabel.frame), CGRectGetWidth(inner.bounds)-16, 40)];
    updataContentLabel.textAlignment = NSTextAlignmentCenter;
    updataContentLabel.textColor = COLOR_FONT_DARK_GRAY;
    updataContentLabel.text = updataMessage;
    updataContentLabel.font = [UIFont systemFontOfSize:13];
    updataContentLabel.numberOfLines = 0;
    [inner addSubview:updataContentLabel];
    
    
    UIView *sep1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(updataContentLabel.frame)+10, 250, 0.5)];
    sep1.backgroundColor = [UIColor lightGrayColor];
    [inner addSubview:sep1];
    
    UIView *sep2 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(inner.bounds)/2,CGRectGetMaxY(sep1.frame), 0.5, 45)];
    sep2.backgroundColor = [UIColor lightGrayColor];
    [inner addSubview:sep2];
    
    UIButton *refuse = [UIButton setDetailsBtn:@"稍后再说" picName:nil btnBackColor:nil titleColor:COLOR_FONT_LIGHT_GREEN];
    refuse.tag = 10;
    refuse.frame = CGRectMake(0,CGRectGetMaxY(sep1.frame), CGRectGetWidth(inner.bounds)/2, 50);
    [inner addSubview:refuse];
    [refuse addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *accept = [UIButton setDetailsBtn:@"立即更新" picName:nil btnBackColor:nil titleColor:COLOR_BUTTON_MAIN_GREEN];
    accept.tag = 11;
    accept.frame = CGRectMake(CGRectGetWidth(inner.bounds)/2, CGRectGetMaxY(sep1.frame),CGRectGetWidth(inner.bounds)/2, 45);
    [inner addSubview:accept];
    [accept addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];

}

/**
 *  取消更新
 */
- (void)cancleAction
{
    windowShadowView.hidden = YES;
}
/**
 *  跳转更新
 */
- (void)sureAction
{
    NSURL *urlUpdate  = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1119078683"];
    [[UIApplication sharedApplication] openURL:urlUpdate];
    windowShadowView.hidden = YES;
}

@end
